/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
//import java.sql.Statement;
//import java.util.List;
//import org.hibernate.*;
//import util.*;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.CuaHang;
import util.NewHibernateUtil;
/**
 *
 * @author 0
 */
public class CuaHangDAO extends BaseDAO{
    
     public static void ThemCuaHang(CuaHang cuaHang) throws Exception
    {

        Them(cuaHang);
    }
     
    public static void XoaCuaHang(Object[] cuaHanglist) throws Exception
    {
         Xoa(cuaHanglist);
    }
    
     public static void SuaCuaHang(CuaHang cuaHang) throws Exception{
        Sua(cuaHang);
    }
     
     public static List<CuaHang> TimKiemCuaHang(Integer maCuaHang,String tenCuaHang,String diaChi,boolean forDialogSearch) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string="from CuaHang ch where (ch.isDelete=false or ch.isDelete=null)";
//        if (nguoiDung.getLoaiNguoiDung().getId() != LoaiNguoiDungEnum.Admin.getCode() || forDialogSearch) {
//            string += String.format(" and spch.cuaHang.id=%d", nguoiDung.getCuaHang().getId());
//        }
        if(maCuaHang!=null){
            string+=String.format(" and ch.id=%d", maCuaHang);
        }
        if(!tenCuaHang.isEmpty()){
            string+=" and lower(ch.tenCuaHang) like :tenCuaHang";
        }
        if(!diaChi.isEmpty()){
             string+=" and lower(ch.diaChi) like :diaChi";
        }
        Query query=session.createQuery(string);
        if(!tenCuaHang.isEmpty()){
            query.setString("tenCuaHang", "%"+tenCuaHang.toLowerCase()+"%");
        }
         if(!diaChi.isEmpty()){
            query.setString("diaChi", "%"+diaChi.toLowerCase()+"%");
        }
        List<CuaHang>cuaHangs=query.list();
        session.close();
        return cuaHangs;
    }

}
